<?php
use tightenco\collect;
//单例
class LogFile{
    //创建静态私有的变量保存该类对象
    static private $instance;
    
    //防止直接创建对象
    private function __construct(){
        
    }
        //防止克隆对象
    private function __clone(){

    }

    static public function getInstance(){
        //判断$instance是否是Uni的对象
        //没有则创建
        if (!self::$instance instanceof self) {
            self::$instance = new self();
        }
        return self::$instance;        
    }

    public function toSelfArray($filename){

		if (file_exists($filename)) {

			$file = fopen($filename, "r");
			$logs=collect([]);
			$i=0;

			while(!feof($file)) {
				$line= fgets($file);//fgets()函数从文件指针中读取一行
				if (!trim($line)) break;
				if (substr(trim($line),0,8)<='20180101') break;
				// iconv('GBK', 'UTF-8', $line); 
				$row=collect([]);
				$row->put('d_date', substr($line, 0, 4).'-'.substr($line, 4, 2).'-'.substr($line, 6, 2));
				$row->put('b_time', substr($line, 8, 2).':'.substr($line, 10, 2).':'.substr($line, 12, 2));
				$row->put('len', (intval(substr($line, 18, 2))*60+intval(substr($line, 20, 2))+floor((intval(substr($line, 22, 2))+59)/60)).'秒');
				$row->put('fre', trim(iconv('GBK', 'UTF-8',substr($line, 24, 16))));
				$row->put('s_time', trim(iconv('GBK', 'UTF-8',substr($line, 40, 16))));
				$row->put('number', trim(iconv('GBK', 'UTF-8',substr($line, 56, 10))));
				$row->put('g_num', str_pad(trim(substr($line, 66, 2)), 2, " ", STR_PAD_LEFT));
				$row->put('content', trim(iconv('GBK', 'UTF-8',substr($line, 68, 50))));
				$row->put('belt', trim(substr($line, 118, 15)));
				$row->put('cx', 0);
				$row->put('rev_cx', 0);
				$logs->push($row);
			}
			fclose($file);
			
			$bcs = $logs->sortBy(function ($log) {
				return $log['d_date'].' '.$log['b_time'];
			});

			$s_time='';
			$cx=0;
			
			$bcs->map(function($bc) use (&$s_time, &$cx) {
				if ($bc['s_time']==$s_time) {
					$cx++;
					$bc['cx']=$cx;
				} else {
					$cx=1;
					$s_time=$bc['s_time'];
					$bc['cx']=1;
				}
				return $bc;
			});
			
			$res = $bcs->reverse();

			$s_time='';
			$rev_cx=0;
			
			$res->map(function($re) use (&$s_time, &$rev_cx) {
				if ($re['s_time']==$s_time) {
					$rev_cx++;
					$re['rev_cx']=$rev_cx;
				} else {
					$rev_cx=1;
					$s_time=$re['s_time'];
					$re['rev_cx']=1;
				}
				return $re;
			});
			
			return $res->reverse();
		}
		return false;
    }

    public function toOtherArray($filename){
    	if (file_exists($filename)) {

			$file = fopen($filename, "r");
			$logs=collect([]);
			$i=0;

			while(!feof($file)) {
				$line= fgets($file);//fgets()函数从文件指针中读取一行
				if (!trim($line)) break;
				if (substr(trim($line),0,8)<='20180101') break;
				// iconv('GBK', 'UTF-8', $line); 
				$row=collect([]);
				$row->put('d_date', substr($line, 0, 4).'-'.substr($line, 4, 2).'-'.substr($line, 6, 2));
				$row->put('b_time', substr($line, 8, 2).':'.substr($line, 10, 2).chr(39).chr(39).substr($line, 12, 2));
				$row->put('len', (intval(substr($line, 18, 2))*60+intval(substr($line, 20, 2))+floor((intval(substr($line, 22, 2))+59)/60)).'秒');
				$row->put('fre', trim(iconv('GBK', 'UTF-8',substr($line, 24, 16))));
				$row->put('s_time', trim(iconv('GBK', 'UTF-8',substr($line, 40, 16))));
				$row->put('number', trim(iconv('GBK', 'UTF-8',substr($line, 56, 10))));
				$row->put('content', trim(iconv('GBK', 'UTF-8',substr($line, 66, 20))));
				$row->put('belt', trim(substr($line, 86, 15)));
				$row->put('g_num', trim(substr($line, 102, 2)));
				$row->put('cx', 0);
				$row->put('rev_cx', 0);
				$logs->push($row);
			}
			fclose($file);
			
			$bcs = $logs->sortBy(function ($log) {
				return $log['d_date'].' '.$log['b_time'];
			});

			$s_time='';
			$cx=0;
			
			$bcs->map(function($bc) use (&$s_time, &$cx) {
				if ($bc['s_time']==$s_time) {
					$cx++;
					$bc['cx']=$cx;
				} else {
					$cx=1;
					$s_time=$bc['s_time'];
					$bc['cx']=1;
				}
				return $bc;
			});
			
			$res = $bcs->reverse();

			$s_time='';
			$rev_cx=0;
			
			$res->map(function($re) use (&$s_time, &$rev_cx) {
				if ($re['s_time']==$s_time) {
					$rev_cx++;
					$re['rev_cx']=$rev_cx;
				} else {
					$rev_cx=1;
					$s_time=$re['s_time'];
					$re['rev_cx']=1;
				}
				return $re;
			});
			
			return $res->reverse();			
		}
		return false;
    }   
}